package com.yikome.shop.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import com.yikome.common.CommonService;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.yikome.shop.entity.Navigation;
import com.yikome.shop.entity.NavigationCollection;
import com.yikome.shop.repository.NavigationCollectionRepository;
import com.yikome.shop.repository.NavigationRepository;

@Service
public class NavigationService extends CommonService<Navigation, Long> {

	@Autowired
	private NavigationRepository navigationRepository ;
	
	@Autowired
	private NavigationCollectionRepository navigationCollectionRepository ; 
	
	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate ; 
	
	@Autowired
	public void setResourceDao(NavigationRepository repository) {
		super.setCommonDao(repository);
	}

	public List<Navigation> findShopNavigation(Long shopId) {
		
		List<Navigation> navigations = navigationRepository.findNavigation(shopId);
		
		return treeNavigation(navigations);
	}

	private List<Navigation> treeNavigation(List<Navigation> navigations) {
		
		List<Navigation> result = new ArrayList<>();
		
		for (Navigation navigation : navigations) {
			if(navigation.getParentId() == null) {
				navigation.setChildrens(findNavigationChildrens(navigation, navigations));
				result.add(navigation);
			}
		}
		return result;
	}
	
	private List<Navigation> findNavigationChildrens(Navigation parent , List<Navigation> navigations){
		
		List<Navigation> childrens = new ArrayList<>();
		for (Navigation navigation : navigations) {
			if(parent.getId().equals(navigation.getParentId())) {
				childrens.add(navigation);
			}
		}
		return childrens;
	}

	@Transactional
	public Boolean deleteNavigation(Long navId) {
		navigationRepository.deleteById(navId);
		navigationCollectionRepository.deleteByNavId(navId);
		return Boolean.TRUE;
	}

	@Transactional
	public NavigationCollection bindNavCollection(NavigationCollection navigationCollection) {
		NavigationCollection source = navigationCollectionRepository.findByNavIdAndCollectionId(navigationCollection.getNavId() , navigationCollection.getCollectionId());
		
		if(source != null) {
			source.setPriority(navigationCollection.getPriority());
			return navigationCollectionRepository.save(source);
		}
		
		return navigationCollectionRepository.save(navigationCollection);
	}
	
	@Transactional
	public List<NavigationCollection> bindNavCollections(@RequestBody List<NavigationCollection> navigationCollections) {
		
		List<NavigationCollection> result = new ArrayList<>();
		
		for (NavigationCollection navigationCollection : navigationCollections) {
			
			Preconditions.checkArgument(navigationCollection.getNavId() != null, "参数信息不合法...");
			Preconditions.checkArgument(navigationCollection.getCollectionId() != null, "参数信息不合法...");
			
			NavigationCollection source = navigationCollectionRepository.findByNavIdAndCollectionId(navigationCollection.getNavId() , navigationCollection.getCollectionId());
			
			if(source != null) {
				source.setPriority(navigationCollection.getPriority());
				result.add(navigationCollectionRepository.save(source));
			}
			result.add(navigationCollectionRepository.save(navigationCollection));
		}
		return result;
	}

	@Transactional
	public Boolean deleteNavCollection(Long navCollectionId) {
		//navigationCollectionRepository.deleteByNavIdAndCollectionId(navigationCollection.getNavId() , navigationCollection.getCollectionId());
		navigationCollectionRepository.deleteById(navCollectionId);
		return Boolean.FALSE;
	}

	public List<Map<String , Object>> findNavCollection(Long navId) {
		
		String contentSQL = "SELECT \n" + 
							"	s_nav_collection.id, s_nav_collection.priority, s_collection.id collectionId, s_collection.`name` collectionName, s_collection.banners, s_collection.comments, s_collection.present_id presentId, s_present.thumb \n" + 
							"FROM s_nav_collection \n" + 
							"	LEFT JOIN s_collection ON (s_nav_collection.collection_id = s_collection.id) \n" + 
							"	LEFT JOIN s_present ON (s_collection.present_id = s_present.id) \n" + 
							"WHERE s_nav_collection.nav_id = :navId \n" + 
							"ORDER BY s_nav_collection.priority ASC \n" ; 
		
		return jdbcTemplate.queryForList(contentSQL, ImmutableMap.of("navId", navId));
	}
	
}
